package com.example.smartcommunity.com.example.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartcommunity.FriendlysocialActivity;
import com.example.smartcommunity.JyActivity;
import com.example.smartcommunity.PropertyActivity;
import com.example.smartcommunity.R;
import com.example.smartcommunity.WaterActivity;
import com.example.smartcommunity.WodekuaidiActivity;
import com.example.smartcommunity.WxfwActivity;
import com.example.smartcommunity.YezhuzizhiActivity;
import com.example.smartcommunity.ZhgjActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class HomeFragment extends Fragment {

    private ImageView iv_guanjia,iv_yezhu,iv_youling;
    private TextView tv_songshui,tv_jiaofei,tv_kuaidi,tv_jianyi,tv_weixiu;

    private ViewPager mViewPaper;
    private List<ImageView> images;
    private List<View> dots;
    private int currentItem;
    //记录上一次点的位置
    private int oldPosition = 0;
    //存放图片的id
    private int[] imageIds = new int[]{
            R.drawable.ly_01,
            R.drawable.ly_02,
            R.drawable.ly_03,
    };
    /**
     * 接收子线程传递过来的数据
     */
    private Handler mHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            mViewPaper.setCurrentItem(currentItem);
        };
    };

    private ViewPagerAdapter adapter;
    private ScheduledExecutorService scheduledExecutorService;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_home, container, false);

        iv_guanjia = view.findViewById(R.id.iv_guanjia);
        iv_yezhu = view.findViewById(R.id.iv_yezhu);
        iv_youling = view.findViewById(R.id.iv_youling);
        tv_songshui = view.findViewById(R.id.tv_songshui);
        tv_jiaofei = view.findViewById(R.id.tv_jiaofei);
        tv_kuaidi = view.findViewById(R.id.tv_kuaidi);
        tv_jianyi = view.findViewById(R.id.tv_jianyi);
        tv_weixiu = view.findViewById(R.id.tv_weixiu);

        iv_guanjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), ZhgjActivity.class);
                startActivity(intent);
            }
        });

        iv_yezhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), YezhuzizhiActivity.class);
                startActivity(intent);
            }
        });

        iv_youling.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), FriendlysocialActivity.class);
                startActivity(intent);
            }
        });

        tv_songshui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), WaterActivity.class);
                startActivity(intent);
            }
        });
        tv_jiaofei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PropertyActivity.class);
                startActivity(intent);
            }
        });
        tv_kuaidi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), WodekuaidiActivity.class);
                startActivity(intent);
            }
        });
        tv_jianyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), JyActivity.class);
                startActivity(intent);
            }
        });
        tv_weixiu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), WxfwActivity.class);
                startActivity(intent);
            }
        });





        mViewPaper = (ViewPager)view.findViewById(R.id.vp);

        //显示的图片
        images = new ArrayList<ImageView>();
        for(int i = 0; i < imageIds.length; i++){
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(imageIds[i]);
            images.add(imageView);
        }
        //显示的小点
        dots = new ArrayList<View>();
        dots.add(view.findViewById(R.id.dot_0));
        dots.add(view.findViewById(R.id.dot_1));
        dots.add(view.findViewById(R.id.dot_2));


        adapter = new ViewPagerAdapter();
        mViewPaper.setAdapter(adapter);
        Log.d("onCreateView", "onCreateView");

        mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageSelected(int position) {
                dots.get(position).setBackgroundResource(R.drawable.doc_01);
                dots.get(oldPosition).setBackgroundResource(R.drawable.doc_02);

                oldPosition = position;
                currentItem = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        return view;
    }

    /**
     * 自定义Adapter
     * @author liuyazhuang
     *
     */
    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
            view.removeView(images.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            // TODO Auto-generated method stub
            view.addView(images.get(position));
            Log.d("init", "" + images.get(position));
            return images.get(position);
        }

    }

    /**
     * 利用线程池定时执行动画轮播
     */
    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleWithFixedDelay(
                new ViewPageTask(),
                2,
                2,
                TimeUnit.SECONDS);
    }


    /**
     * 图片轮播任务
     * @author liuyazhuang
     *
     */
    private class ViewPageTask implements Runnable{

        @Override
        public void run() {
            currentItem = (currentItem + 1) % imageIds.length;
            mHandler.sendEmptyMessage(0);
        }
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if(scheduledExecutorService != null){
            scheduledExecutorService.shutdown();
            scheduledExecutorService = null;
        }
    }

}